package com.twx;

import redis.clients.jedis.JedisPubSub;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;

public class MyJedisPubSub extends JedisPubSub {

    @Override
    public void onMessage(String channel, String message) {

        System.out.println("【channel】 "+channel+"\n【message】 "+message);
        try {
            writeInFile("【channel】 "+channel+"\n 【message】 "+message,"message.txt");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private void writeInFile(String message, String fileName) throws Exception{
        File file = new File(fileName);
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file,true)));
        writer.write(message);
        writer.newLine();
        writer.close();
    }
}
